#!/usr/bin/env sh
#
# wm independent hotkeys
#

# terminal emulator
super + Return
    wezterm

super + shift + Return
    bspc rule -a org.wezfurlong.wezterm -o state=floating follow=on center=true && wezterm

# program launcher
super + @space
    rofi -modes "drun,window,run" -show drun

# make sxhkd reload its configuration files:
super + Escape
    pkill -USR1 -x sxhkd && dunstify "sxhkd" "Reloaded successfully"

#
# bspwm hotkeys
#
# rotate the whole desktop
super + shift + r
    bspc node @/ -R 90

# rotate the parent of current focused
super + r
    bspc node @parent -R 90

# hide window
super + v
    bspc node -g hidden

# unhide window
super + shift + v
    bspc node {,$(bspc query -N -n .hidden | tail -n1)} -g hidden=off

# unhide window with rofi
super + alt + v
    ~/dotfiles/scripts/unhidewindow

# hide/unhide all windows
super + d
    bspc query -N -n .local.window | xargs -I ID bspc node ID -g hidden

# focus a floating window from a tiled window
super + a
    if [ "$(bspc query -N -n focused.floating)" ]; then \
        bspc node -f last.tiled.local; \
    else \
        bspc node -f last.floating.local; \
    fi

# quit/restart bspwm
super + alt + {q,r}
    bspc {quit,wm -r}

# close and kill
super + {_,shift + }w
    bspc node -{c,k}

# alternate between the tiled and monocle layout
super + m
    bspc desktop -l next

# send the newest marked node to the newest preselected node
super + y
    bspc node newest.marked.local -n newest.!automatic.local

#
# state/flags
#

# toggle the window state
super + {t,shift + t,s,f}
    bspc node -t ~{tiled,pseudo_tiled,floating,fullscreen}

# set the node flags
super + ctrl + {m,x,y,z}
    bspc node -g {marked,locked,sticky,private}

#
# focus/swap
#
 
# focus/swap the node in the given direction
super + {_,shift + }{h,j,k,l}
    bspc node -{f,s} {west,south,north,east}

# focus the next/previous desktop in the current monitor
super + bracket{left,right}
    bspc desktop -f {prev,next}.local

# focus the last node/desktop
super + {Tab,grave}
    bspc {node,desktop} -f last.local

# focus or send to the given desktop
super + {_,shift + }{1-9,0}
    bspc {desktop -f,node -d} '^{1-9,10}'

#
# preselect/receptacle
#

# close all receptacle
super + alt + space
    for win in `bspc query -N -n .leaf.\!window`; do bspc node $win -k ; done;

# insert receptacle
super + alt + {Left,Down,Up,Right}
    bspc node --presel-dir {west,south,north,east} -i

# move to rectacle
super + e
    ID=$(bspc query -N -n); \
    bspc node $ID -n $(bspc query -N -n .leaf.\!window)

# preselect the direction
super + ctrl + {Left,Down,Up,Right}
    bspc node -p {west,south,north,east}

# preselect the ratio
super + ctrl + {1-9}
    bspc node -o 0.{1-9}

# cancel the preselection for the focused node
super + ctrl + space
    bspc node -p cancel

# cancel the preselection for the focused desktop
super + ctrl + shift + space
    bspc query -N -d | xargs -I id -n 1 bspc node id -p cancel

#
# move/resize
#

# expand/contract a window by moving one of its side outward/inward
super + q : {Left,Down,Up,Right}
    STEP=20; SELECTION={1,2,3,4}; \
    bspc node -z $(echo "left -$STEP 0,bottom 0 $STEP,top 0 -$STEP,right $STEP 0" | cut -d',' -f$SELECTION) || \
    bspc node -z $(echo "right -$STEP 0,top 0 $STEP,bottom 0 -$STEP,left $STEP 0" | cut -d',' -f$SELECTION)

# expand a window by moving one of its side outward
super + q : {h,j,k,l}
    bspc node -z {left -20 0,bottom 0 20,top 0 -20,right 20 0}

# contract a window by moving one of its side inward
super + q : shift + {h,j,k,l}
    bspc node -z {right -20 0,top 0 20,bottom 0 -20,left 20 0}

# move a floating window
super + {Left,Down,Up,Right}
    bspc node -v {-30 0,0 30,0 -30,30 0}

# screenshot
Print
    ~/dotfiles/scripts/sshot -m select

super + Print
    ~/dotfiles/scripts/sshot -m win

shift + Print
    ~/dotfiles/scripts/sshot -m full

# send win to next or prev desktop & focus it
alt + ctrl + shift + { Right,Left }
    DIR={next.local,prev.local}; \
    bspc node -d "$DIR" --follow

# open app fast
super + alt + {b, f, t, s, m, M, a, g}
    { bspc rule -a org.wezfurlong.wezterm -o state=floating follow=on center=true && wezterm start -- btop, \
      firefox, \
      telegram-desktop, \
      simplescreenrecorder, \
      ~/dotfiles/rofi/mpd/mpd, \
      bspc rule -a org.wezfurlong.wezterm -o state=floating follow=on center=true && wezterm start -- "ncmpcpp", \
      ~/dotfiles/scripts/showkey, \
      gromit-mpx &
    }

# move the floating focused window to center
super + ctrl + c
    ~/dotfiles/scripts/tocenter

# powermenu
ctrl + super + alt + p
    eww open powermenu

# screen lock
ctrl + super + alt + l
    betterlockscreen -l dim

# close screen
ctrl + super + alt + x
    sleep 1; xset dpms force standby

# hide/show bar
super + ctrl + t
    eww open-many --toggle bar

super + ctrl + r
    ~/dotfiles/eww/toggle_bar


# make split ratios equal
super + equal
    bspc node @/ --equalize

# make split ratios balanced
super + minus
    bspc node @/ --balance

# volume
XF86Audio{LowerVolume,RaiseVolume,Mute}
    ~/dotfiles/scripts/volume_dunst {down,up,mute}

# vim: ft=sh tabstop=4
